package com.joysuch.wwyt.mh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.mh.bean.MhAlarmLevelConfigBean;
import com.joysuch.wwyt.mh.entity.MhAlarmLevelConfig;
import com.joysuch.wwyt.mh.enums.AlarmLevelNameEnum;
import com.joysuch.wwyt.mh.mapper.MhAlarmLevelConfigMapper;
import com.joysuch.wwyt.mh.service.MhAlarmLevelConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Leong
 * @date 2022/9/19 16:23
 */
@Service
public class MhAlarmLevelConfigServiceImpl extends ServiceImpl<MhAlarmLevelConfigMapper, MhAlarmLevelConfig> implements MhAlarmLevelConfigService {

    @Override
    public List<MhAlarmLevelConfig> findList(Long tenentId, String orgCode) {
        return baseMapper.findList(tenentId,orgCode);
    }

    @Override
    @Transactional
    public void updateLevel(List<MhAlarmLevelConfigBean> beans) {
        if (CollectionUtils.isNotEmpty(beans)){
            beans.forEach(bean -> {
                MhAlarmLevelConfig config = baseMapper.selectById(bean.getId());
                if (config != null){
                    config.setAlarmLevel(bean.getAlarmLevel());
                    config.setLevel(bean.getLevel());
                    config.setLevelName(AlarmLevelNameEnum.find(bean.getLevel()).getDesc());
                    baseMapper.updateById(config);
                }
            });
        }
    }

    @Override
    public Map<Integer, String> findLevelMap(Long tenentId, String orgCode) {
        Map<Integer, String> map = new HashMap<>();
        List<MhAlarmLevelConfig> list = baseMapper.findList(tenentId,orgCode);
        if (CollectionUtils.isNotEmpty(list)){
            map = list.stream().collect(Collectors.toMap(MhAlarmLevelConfig::getAlarmLevel,MhAlarmLevelConfig::getLevelName));
        }
        return map;
    }
}
